package com.eh.session;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by David Li on 2016/5/19.
 */
@WebServlet("/cookie.login.do")
public class CookieLoginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String auto = req.getParameter("auto");
        String username = req.getParameter("username");
        if (auto != null && Boolean.parseBoolean(auto)) {
            Cookie cookie = new Cookie("username", username);
            cookie.setMaxAge(100);
            cookie.setHttpOnly(true);
            resp.addCookie(cookie);
        }
        req.setAttribute("username", username);
        req.getRequestDispatcher("/cookie.member.do").forward(req, resp);
    }

}
